//
//  ViewController.m
//  ChatDemo
//
//  Created by YuP on 16/5/11.
//  Copyright © 2016年 SporeTech. All rights reserved.
//

/*
   spore-tech#sporetech

 Client Id:	YXA6QWJyABcdEea5YYcGI5SBrw
 Client Secret:	YXA6kmilNuvcJbk5s2fwzcdJLRA4E1I
 */



#import "ViewController.h"
#import "AppDelegate.h"

@interface ViewController ()<EMChatManagerDelegate>
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextView *messageTextfield;
@property (weak, nonatomic) IBOutlet UITextView *receiveTextfield;

@property (weak, nonatomic) IBOutlet UITextField *userNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *passWordTextField;

@property (nonatomic) NSString *user;
@property (nonatomic) NSString *passWord;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 设置代理
    [[EMClient sharedClient].chatManager addDelegate:self delegateQueue:nil];
    
}


// 点击发送按钮
- (IBAction)sendBtn:(id)sender {
    NSString *dateStr = [NSString stringWithFormat:@"%@",[NSDate date]];
    NSArray *array = [dateStr componentsSeparatedByString:@"+"];
    
    NSString *str =  [NSString stringWithFormat:@"时间：%@\n消息来自:%@\n内容为:%@",array[0],_user,_messageTextfield.text];
    
    EMTextMessageBody *body = [[EMTextMessageBody alloc] initWithText:str];
    NSString *from = [[EMClient sharedClient] currentUsername];
    //生成Message
    EMMessage *message = [[EMMessage alloc] initWithConversationID:@"6001" from:from to:_nameTextField.text  body:body ext:nil];
    message.chatType = EMChatTypeChat;// 设置为单聊消息
    //message.chatType = EMChatTypeGroupChat;// 设置为群聊消息
    //message.chatType = EMChatTypeChatRoom;// 设置为聊天室消息
    
    // 发送消息（异步方法）
    [[EMClient sharedClient].chatManager asyncSendMessage:message progress:nil completion:^(EMMessage *aMessage, EMError *aError) {
        NSLog(@"消息发送完成");
    }];
    
}

// 点击注册按钮
- (IBAction)registerBtn:(id)sender{
    EMError *error = [[EMClient sharedClient] registerWithUsername:_userNameTextField.text password:_passWordTextField.text];
    if (error==nil) {
        NSLog(@"注册成功");
    }
}

// 点击登录按钮
- (IBAction)loginBtn:(id)sender{
    EMError *error = [[EMClient sharedClient] loginWithUsername:_userNameTextField.text password:_passWordTextField.text];
    if (!error) {
        NSLog(@"登录成功");
    }
    _user = _userNameTextField.text;
    _passWord = _passWordTextField.text;
}

// 自动登录
- (void)autoLogin{
    EMError *error = [[EMClient sharedClient] loginWithUsername:_userNameTextField.text password:_passWordTextField.text];
    if (!error)
    {
        [[EMClient sharedClient].options setIsAutoLogin:YES];
    }
}

// 自动登录回调
- (void)didAutoLoginWithError:(EMError *)aError{
    if (aError == nil) {
        NSLog(@"自动登录成功");
    }
}

// 退出登录
- (void)logout{
    EMError *error = [[EMClient sharedClient] logout:YES];
    if (!error) {
        NSLog(@"退出成功");
    }
}

// 消息送达回执
- (void)didReceiveHasDeliveredAcks:(NSArray *)aMessage{
    NSLog(@"消息已经送达");
}

// 获取会话
- (void)creatCaht{
    [[EMClient sharedClient].chatManager getConversation:@"8001" type:EMConversationTypeChat createIfNotExist:YES];
}

// 代理方法 收到消息的回调，带有附件类型的消息可以用SDK提供的下载附件方法下载（后面会讲到）
- (void)didReceiveMessages:(NSArray *)aMessages
{
    for (EMMessage *message in aMessages) {
        EMMessageBody *msgBody = message.body;
        switch (msgBody.type) {
            case EMMessageBodyTypeText:
            {
                // 收到的文字消息
                EMTextMessageBody *textBody = (EMTextMessageBody *)msgBody;
                NSString *txt = textBody.text;
                NSLog(@"收到的文字是 txt -- %@",txt);
                // 消息显示在界面上
                _receiveTextfield.text = txt;
            }
                break;
            case EMMessageBodyTypeImage:
            {
//                // 得到一个图片消息body
//                EMImageMessageBody *body = ((EMImageMessageBody *)msgBody);
//                NSLog(@"大图remote路径 -- %@"   ,body.remotePath);

            }
                break;
            case EMMessageBodyTypeLocation:
            {
//                EMLocationMessageBody *body = (EMLocationMessageBody *)msgBody;
//                NSLog(@"纬度-- %f",body.latitude);

            }
                break;
            case EMMessageBodyTypeVoice:
            {
                // 音频sdk会自动下载
//                EMVoiceMessageBody *body = (EMVoiceMessageBody *)msgBody;
//                NSLog(@"音频remote路径 -- %@"      ,body.remotePath);

            }
                break;
            case EMMessageBodyTypeVideo:
            {
//                EMVideoMessageBody *body = (EMVideoMessageBody *)msgBody;
                
//                NSLog(@"视频remote路径 -- %@"      ,body.remotePath);

            }
                break;
            case EMMessageBodyTypeFile:
            {
//                EMFileMessageBody *body = (EMFileMessageBody *)msgBody;
//                NSLog(@"文件remote路径 -- %@"      ,body.remotePath);

            }
                break;
                
            default:
                break;
        }
    }
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
@end

















//